class ShopNotification < ApplicationRecord
  enum notify_type: [:normal, :warning, :urgent]

  belongs_to :shop
  counter_culture :shop, column_name: proc { |model| model.read_at.blank? ? 'unread_shop_notifications_count' : nil }

  def self.notify_shop_created
    shop_ids = Shop.select(:id).where(state: :opening)
    return if shop_ids.count <= 0

    ShopNotification.bulk_insert(set_size: shop_ids.count) do |worker|
      shop_ids.each do |sid|
        logger.debug "Post Notification to: #{sid}"
        worker.add(
          shop_id: sid,
          notify_type: :normal,
          title: "",
          content: ""
        )
      end
    end
  end

  def async_create_shop_notify
    NotifyShopJob.perform_later
  end
end
